﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MQBasic.Config;
using MQBasic.Entity;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace MQConsumer
{
    class Program
    {
        static void Main(string[] args)
        {
            int queCount = 10;// 消费者队列个数

            for (int i = 0; i < queCount; i++)
            {
                ThreadPool.QueueUserWorkItem(ConsumGSPMessage, i);
            }

            Console.ReadKey();


        }

        static void ConsumGSPMessage(object queName)
        {
            ConnectionFactory factory = new ConnectionFactory
            {
                HostName = MQConfig.MqHost,
                UserName = MQConfig.MqUserName,
                Password = MQConfig.MqPwd,
                Port = MQConfig.MqPort
            };

            //using (IConnection conn = factory.CreateConnection())
            //{
            //    using (IModel im = conn.CreateModel())
            //    {

            //        while (true)
            //        {
            //            try
            //            {
            //                BasicGetResult res = im.BasicGet(MQConfig.QueueName +"_" + queName.ToString(), false);
            //                if (res != null)
            //                {
            //                    string json = UTF8Encoding.UTF8.GetString(res.Body);
            //                    Console.WriteLine("消费消息：{0}", json);
            //                    im.BasicAck(res.DeliveryTag, false);
            //                }
            //            }
            //            catch (Exception ex)
            //            {
            //                Console.WriteLine(ex.Message);
            //            }
            //            //Thread.Sleep(1);
            //        }
            //    }
            //}

            using (IConnection conn = factory.CreateConnection())
            {
                using (IModel im = conn.CreateModel())
                {
                    string queueName = MQConfig.QueueName + "_" + queName.ToString();
                    im.QueueDeclare(queue: queueName,
                                     durable: true,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                    var consumer = new EventingBasicConsumer(im);

                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        im.BasicAck(ea.DeliveryTag, false);

                        Console.WriteLine(" [x] Received {0}", message);
                        
                    };

                    im.BasicConsume(queue: queueName,
                                     autoAck: false,
                                     consumerTag:string.Empty,
                                     noLocal:false,
                                     exclusive:false,
                                     arguments:null,
                                     consumer: consumer);

                }
            }
        }
    }
}